from pathlib import Path
from PIL import Image

d1 = r'M:\图\网图'
pic_ext = ['jpg', 'png', 'gif', 'bmp', 'jpeg', 'webp']
Image.MAX_IMAGE_PIXELS = 1000000000


def format_chinese_number(num):
    units = ['', '万', '亿', '兆']
    unit_index = 0
    formatted_number = ''
    if num == 0:
        return '0'
    while num >= 10000:
        unit_index += 1
        num /= 10000
    formatted_number = "{:.1f}".format(num).rstrip('0').rstrip('.')  # 格式化输出，保留1位小数，但会去掉不必要的小数部分
    formatted_number += units[unit_index]  # 添加单位
    return formatted_number


size_dict = {}  # key=文件名，value=像素数
for x in Path(d1).glob('*.*'):
    if x.suffix.lower()[1:] in pic_ext:
        w, h = 0, 0
        try:
            im = Image.open(x)
            w, h = im.size
            im.close()
            # print(x.name, w, h)
        except Exception as e:
            print(e)
        size_dict[x.name] = w * h
# 对size_dict逆向排序
size_dict = {k: v for k, v in sorted(size_dict.items(), key=lambda item: item[1], reverse=True)}

for k, v in size_dict.items():
    print(format_chinese_number(v), k)
